<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        button {
            margin: 20px;
            width: 100px;
            height: 50px;
        }

        .current {
            background-color: red;
        }
    </style>
</head>

<body>
    <button>按钮1</button>
    <button>按钮2</button>
    <button>按钮3</button>
    <button>按钮4</button>
    <button>按钮5</button>

    <script>
        btn = document.getElementsByTagName('button')

        for (let i = 0; i < btn.length; i++) {
            btn[i].onmouseover = function () {

                //遍历清除掉所有btn的类
                for (let j = 0; j < btn.length; j++) {
                    btn[j].className = ''
                }
                //清除完毕之后再添加类名
                this.className = 'current'
            }

        }

        //鼠标移开清除背景色
        for (let i = 0; i < btn.length; i++) {
            btn[i].onmouseout = function(){
                this.className=''
            }

        }
        
    </script>

</body>

</html>